<?php
namespace Entities\Financeiro;
use Doctrine\ORM\Query;
use Doctrine\ORM\EntityRepository;

class LancamentoRepository extends EntityRepository
{
	public function listar($params = array())
	{
		$qb		= $this->getEntityManager()->createQueryBuilder();
		$qb->select('l')
		->from('Entities\Financeiro\Lancamento', 'l');

		$where	= $qb->expr()->andX();
		$where->add($qb->expr()->eq(1,1));
		foreach ($params as $key => $value)
		{
			if ($key == 'dtinicial')
			{
				$value	= \DateTime::createFromFormat('d/m/Y', $value);
				$where->add($qb->expr()->gte('l.dtvencimento', "'{$value->format('Y-m-d')}'"));
			}
			else if ($key == 'dtfinal')
			{
				$value	= \DateTime::createFromFormat('d/m/Y', $value);
				$where->add($qb->expr()->lte('l.dtvencimento', "'{$value->format('Y-m-d')}'"));
			}
			else
				$where->add($qb->expr()->eq($key, $value));
		}
		$qb->where($where)
		->orderBy('l.dtvencimento');
		return $this->getEntityManager()->createQuery($qb->getDQL());
	}
}